<?php
/**
* @package phpBB3 ATK - Admin Tool Kit
* @version $Id: atk_tool.php 195 2009-01-16 15:41:29Z ej.frerejean $
* @copyright (c) 2008 Admin Tool Kit community development team
* @author Erik Frèrerejean (Erik Frèrejean)
* @author Nathon Guse (EXreaction)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* @ignore
*/
if (!defined('IN_ATK'))
{
	exit;
}

/**
* Main tool class
* This class should be extended by every tool that gets added to the ATK and 
* will profide some basic functionallity
*/
class atk_tool
{
	/**
	* Placeholder for a referance to the ATK core
	*
	* @access private
	*/
	var $atk = null;
	
	/**
	* The string that will be used as form key. %s will be replaced with the tools name
	*
	* @access private
	*/
	var $form_key = 'atk_tool_%s_form_key';
	
	/**
	* The tool information
	*
	* @access private
	*/
	var $tool_info = array();
	
	/**
	* The tool options
	*
	* @access protected
	*/
	var $tool_options = array();

	/**
	* An array holding all the errors that acure during the exection of this tool
	*
	* @access private
	*/
	var $tool_errors = array();
	
	/**
	* Constructor
	* Requires an array with all tool specific data
	*
	* @param array $tool_data
	* @param array $child_class The name of the child class
	* @access protected
	*/
	function __construct($tool_data, $tool_options, $child_class)
	{
		global $atk, $user;

		// A referance to the ATK core
		$this->atk = $atk;
		
		// Assign the given data to the correct vars
		$this->tool_info	= array(
			'CATEGORY'	=> strtoupper($tool_data['CATEGORY']),
			'EXPLAIN'	=> (isset($user->lang[$tool_data['TOOL'] . '_EXPLAIN'])) ? $user->lang[$tool_data['TOOL'] . '_EXPLAIN'] : '',
			'TITLE'		=> (isset($user->lang[$tool_data['TOOL']])) ? $user->lang[$tool_data['TOOL']] : $tool_data['TOOL'],
			'TOOL_NAME'	=> $child_class,
		);
		
		// Load the language files:
		$this->_load_tool_language_files();

		// Load the libraries related to this tool
		$this->_load_tool_libraries();

		// Assign the tool options array
		if (!is_array($tool_options))
		{
			$tool_options = array();
		}

		$this->tool_options = $tool_options;

		// Add a form key
		if (!ATK_PANIC)
		{
			add_form_key(sprintf($this->form_key, $this->tool_info['TOOL_NAME']));
		}
	}

	/**
	* Load all the language files that are related to this tool
	* This are all the files inside the language/../tools/ directory that contain the tools name
	*/
	function _load_tool_language_files()
	{
		// We search in the en dir as this langauge should be here
		if ($handle = opendir(ATK_LANGUAGE_PATH . 'en/tools/'))
		{
			while (false !== ($file_name = readdir($handle)))
			{
				// Skip *nix hidden files
				if (substr($file_name, 0, 1) == '.')
				{
					continue;
				}
				
				// Name 
				$file = substr($file_name, 0, -(strlen(PHP_EXT) + 1));

				// Related to this tool
				if (strpos($this->tool_info['TOOL_NAME'], $file) !== false)
				{
					$this->atk->phpbb->atk_add_lang('tools/' . $file);
				}
			}
		
			closedir($handle);
		}
	}

	/**
	* Load all the library  files that are related to this tool
	* This are all the files inside the includes/lib/tools/ directory that contain the tools name
	*/
	function _load_tool_libraries()
	{
		if ($handle = opendir(ATK_LIB_PATH . 'tools/'))
		{
			while (false !== ($file_name = readdir($handle)))
			{
				// Skip *nix hidden files
				if (substr($file_name, 0, 1) == '.')
				{
					continue;
				}

				// Name
				$file = substr($file_name, 0, -(strlen(PHP_EXT) + 1));

				// Related to this tool
				if (strpos($this->tool_info['TOOL_NAME'], $file) !== false)
				{
					include (ATK_LIB_PATH . 'tools/' . $file_name);
				}
			}
			
			closedir($handle);
		}
	}
	
	/**
	* Validate the form_key for the given tool
	* We let the check_form_key function trigger the error on failer
	*
	* @access protected
	*/
	function check_tool_key()
	{
		check_form_key(sprintf($this->form_key, $this->tool_info['TOOL_NAME']), false, append_sid(ATK_ROOT_PATH . 'index.' . PHP_EXT, array('t' => $this->tool_info['TOOL_NAME'])), true);
	}
	
	/**
	* Get the category of this tool
	*
	* @return String $tool_info['CATEGORY']
	* @access public
	*/
	function get_category()
	{
		return $this->tool_info['CATEGORY'];
	}
	
	/**
	* Get the tool information
	*
	* @return array $tool_info
	* @access public
	*/
	function get_info()
	{
		return $this->tool_info;
	}

	/**
	* Get the tool options
	*
	* @return array $tool_options
	* @access public
	*/
	function get_options()
	{
		return $this->tool_options;
	}

	/**
	* Get the errors generated by this tool
	*
	* @return array $tool_errors
	* @access public
	*/
	function get_tool_errors()
	{
		return (sizeof($this->tool_errors)) ? $this->tool_errors : array();
	}
}
?>